package com.navinfo.opentsp.platform.computing.service.huashen;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.navinfo.opentsp.platform.computing.anno.RoutingProtocol;
import com.navinfo.opentsp.platform.computing.dto.PcodeFaultCodeDto;
import com.navinfo.opentsp.platform.computing.enums.ProtocolEnum;
import com.navinfo.opentsp.platform.computing.enums.YardEnum;
import com.navinfo.opentsp.platform.computing.service.IProtocolParseService;
import com.navinfo.opentsp.platform.computing.util.DateUtils;
import com.navinfo.opentsp.platform.location.protocol.common.LCObdInformation;
import org.apache.flume.Event;

import java.util.List;

@RoutingProtocol(yard = YardEnum.HUASHEN, protocolType = ProtocolEnum.OBD_DATA)
public class ObdDataHuashenService implements IProtocolParseService {
    @Override
    public String parseProtocol(Event event) {
        try {
            //拼写落盘到HDFS的字符串。字段间分隔符是\001，null通过\N表示
            LCObdInformation.ObdInformation obdInformation = LCObdInformation.ObdInformation.parseFrom(event.getBody());
            StringBuilder obdInformationStr = new StringBuilder(5000);
            obdInformationStr.append(obdInformation.getProtocolType()).append(splitStr);
            obdInformationStr.append(obdInformation.getMilType()).append(splitStr);
            obdInformationStr.append(obdInformation.getDiagnosticSupportStatus()).append(splitStr);
            obdInformationStr.append(obdInformation.getDiagnosticReadinessStatus()).append(splitStr);
            obdInformationStr.append(obdInformation.getCalid()).append(splitStr);
            obdInformationStr.append(obdInformation.getVin()).append(splitStr);
            obdInformationStr.append(obdInformation.getCalibrationIdentifyNO()).append(splitStr);
            obdInformationStr.append(obdInformation.getCvn()).append(splitStr);
            obdInformationStr.append(JSON.toJSON(obdInformation.getIuprList())).append(splitStr);
            obdInformationStr.append(obdInformation.getFaultCount()).append(splitStr);
            obdInformationStr.append(JSON.toJSON(obdInformation.getFaultCodesList())).append(splitStr);
            obdInformationStr.append(obdInformation.getIsPatch()).append(splitStr);
            obdInformationStr.append(obdInformation.getGpsTime()).append(splitStr);
            obdInformationStr.append(obdInformation.getEncrypt()).append(splitStr);
            obdInformationStr.append(obdInformation.getTerminalVersion()).append(splitStr);
            obdInformationStr.append(obdInformation.getTerminalId()).append(splitStr);
            obdInformationStr.append(obdInformation.getSerialNumber()).append(splitStr);
            obdInformationStr.append(obdInformation.getRLength()).append(splitStr);
            obdInformationStr.append(obdInformation.getSLength()).append(splitStr);
            obdInformationStr.append(obdInformation.getRValue()).append(splitStr);
            obdInformationStr.append(obdInformation.getSValue()).append(splitStr);
//            obdInformationStr.append(JSON.toJSON(getSpnFmiFaultString(obdInformation.getSpnFmiFaultCodeList()))).append(splitStr);
//            obdInformationStr.append(JSON.toJSON(getPcodeFaultCodeString(obdInformation.getPcodeFaultCodecList()))).append(splitStr);
            //添加字段只能在分割线之上添加,注意时间分区这块，终端传的时候是秒还是毫秒
            //------------------------------------------分割线----------------------------------------
            obdInformationStr.append(DateUtils.format(obdInformation.getGpsTime() * 1000, "yyyyMMdd"));
            return obdInformationStr.toString();
        } catch (Exception e) {
            throw new RuntimeException("执行HDFS的HDFSObdInformationSerializer拦截器失败：" + e.getMessage(), e);
        }
    }

//    public static List<String> getPcodeFaultCodeString(List<LCObdInformation.PcodeFaultCode> pcodeFaultList){
//        List<String> pcodeFaults = Lists.newArrayList();
//        if(pcodeFaultList == null){
//            return pcodeFaults;
//        }
//        for(LCObdInformation.PcodeFaultCode pcodeFault : pcodeFaultList){
//            pcodeFaults.add(PcodeFaultCodeDto.parseString(pcodeFault));
//        }
//        return pcodeFaults;
//    }
//    public static List<String> getSpnFmiFaultString(List<LCObdInformation.SpnFmiFaultCode> spnFmiFaultList){
//        List<String> spnFmis = Lists.newArrayList();
//        if(spnFmiFaultList == null){
//            return spnFmis;
//        }
//        for(LCObdInformation.SpnFmiFaultCode spnFmi: spnFmiFaultList){
//            spnFmis.add(PcodeFaultCodeDto.parseString(spnFmi));
//        }
//        return spnFmis;
//    }
}
